import java.io.*;
import java.util.StringTokenizer;

public class FileFormat
{
    public FileFormat()
    {
    }

    public void write(Judicator k, String out)
    {
        try
        {
            File f = new File(out);
            PrintWriter pw = new PrintWriter(f);

            pw.write("title: " + k.getTitle() + "\n\n");
            pw.write("students\n{\n");
            for (int i = 0; i < k.students.size(); i++)
            {
                Student s = k.students.get(i);
                String name = s.name;
                name = name.trim();
                name = name.replace('(','{');
                name = name.replace(')','}');
                pw.write("\t"+name+" ("+s.email+")["+s.preferredFormat+"]\n");
            }
            pw.write("}\n\n");

            pw.write("teams\n{\n");
            for (int i = 0; i < k.teams.size(); i++)
            {
                Team t = k.teams.get(i);
                String name = t.name;
                name = name.trim();
                name = name.replace('(','{');
                name = name.replace(')','}');
                pw.write("\t"+name+" (");

                for (int j = 0; j < t.students.size(); j++)
                {
                    int index = k.students.indexOf(t.students.get(j));
                    if (index > -1)
                    {
                        pw.write(""+index);
                        if (j < t.students.size() - 1) pw.write(" ");
                    }
                }

                pw.write(")\n");
            }
            pw.write("}\n\n");

            pw.write("tasks\n{\n");
            for (int i = 0; i < k.tasks.size(); i++)
            {
                Task t = k.tasks.get(i);
                String name = t.name;
                name = name.trim();
                name = name.replace('(','{');
                name = name.replace(')','}');
                pw.write("\t"+name+" ("+t.value+")\n");
            }
            pw.write("}\n\n");

            pw.write("issues\n{\n");
            for (int i = 0; i < k.issues.size(); i++)
            {
                Issue s = k.issues.get(i);

                pw.write("\tissue\n\t{\n");
                pw.write("\t\ttask: ");
                if (s.task == null) pw.write("-1");
                else pw.write(""+k.tasks.indexOf(s.task));
                pw.write("\n");

                pw.write("\t\tvalue: " + s.value + "\n");

                pw.write("\t\tteams: ");
                for (int j = 0; j < s.teams.size(); j++)
                {
                    pw.write(k.teams.indexOf(s.teams.get(j)) + " ");
                }
                pw.write("\n");
                pw.write("\t\tdescription\n");
                pw.write("\t\t{\n");

                String[] lines = s.comment.split("\n");
                for (int j = 0; j < lines.length; j++)
                {
                    pw.write("\t\t\t" + lines[j] + "\n");
                }

                pw.write("\t\t}\n");
                pw.write("\t}\n\n");
            }
            pw.write("}\n\n");


            pw.flush();
            pw.close();
        }
        catch (FileNotFoundException e)
        {
            AlertSender.sendAlert("Unable to write to "+out);
        }
    }

    public void read(Judicator k, String in)
    {
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(in));
            String line = br.readLine();
            k.clearAll();

            while(line != null)
            {
                if (line.startsWith("title"))
                {
                    k.setTitle(line.substring(7));
                }
                if (line.startsWith("students"))
                {
                    br.readLine(); // opening brace
                    String l2 = br.readLine();
                    while (!l2.startsWith("}"))
                    {
                        Student s = new Student();
                        s.name = l2.substring(1,l2.indexOf("(")-1);
                        s.email = l2.substring(l2.indexOf("(")+1, l2.indexOf(")"));

                        if (l2.indexOf("[") != -1)
                            s.preferredFormat = Integer.parseInt(l2.substring(l2.indexOf("[")+1, l2.indexOf("]")));
                        
                        k.addStudent(s);

                        l2 = br.readLine();
                    }
                }
                else if (line.startsWith("teams"))
                {
                    br.readLine(); // opening brace
                    String l2 = br.readLine();
                    while (!l2.startsWith("}"))
                    {
                        Team t = new Team();
                        t.name = l2.substring(1,l2.indexOf("(")-1);
                        String members = l2.substring(l2.indexOf("(")+1, l2.indexOf(")"));
                        StringTokenizer st = new StringTokenizer(members);
                        while (st.hasMoreTokens())
                        {
                            int index = Integer.parseInt(st.nextToken());
                            Student s = k.students.get(index);
                            t.addStudent(s);
                        }
                        k.addTeam(t);

                        l2 = br.readLine();
                    }
                }
                else if (line.startsWith("tasks"))
                {
                    br.readLine(); // opening brace
                    String l2 = br.readLine();
                    while (!l2.startsWith("}"))
                    {
                        Task t = new Task();
                        t.name = l2.substring(1,l2.indexOf("(")-1);
                        t.value = Double.parseDouble(l2.substring(l2.indexOf("(")+1, l2.lastIndexOf(")")));
                        k.addTask(t);

                        l2 = br.readLine();
                    }
                }
                else if (line.startsWith("issues"))
                {
                    br.readLine();
                    String l2 = br.readLine();
                    while (!l2.startsWith("}"))
                    {
                        if (l2.startsWith("\tissue"))
                        {
                            Issue s = new Issue();

                            br.readLine();
                            l2 = br.readLine();
                            while (!l2.startsWith("\t}"))
                            {
                                if (l2.startsWith("\t\ttask: "))
                                {
                                    int task = Integer.parseInt(l2.substring(8));
                                    if (task < 0) s.task = null;
                                    else s.task = k.tasks.get(task);
                                }
                                else if (l2.startsWith("\t\tvalue: "))
                                {
                                    s.value = Double.parseDouble(l2.substring(9));
                                }
                                else if (l2.startsWith("\t\tteams: "))
                                {
                                    StringTokenizer st = new StringTokenizer(l2.substring(9));
                                    while (st.hasMoreTokens())
                                    {
                                        int t = Integer.parseInt(st.nextToken());
                                        s.addTeam(k.teams.get(t));
                                    }
                                }
                                else if (l2.startsWith("\t\tdescription"))
                                {
                                    br.readLine();
                                    String l = br.readLine();
                                    while (!l.startsWith("\t\t}"))
                                    {
                                        if (l.length() > 2)
                                        {
                                            if (s.comment == null)
                                                s.comment = l.substring(3) + "\n";
                                            else
                                                s.comment = s.comment + l.substring(3) + "\n";
                                        }
                                        l = br.readLine();
                                    }
                                    if (s.comment != null)
                                    {
                                        s.comment = s.comment.substring(0, s.comment.length() - 1);
                                    }
                                }
                                l2 = br.readLine();
                            }
                            k.addIssue(s);
                        }
                        l2 = br.readLine();
                    }
                }
                line = br.readLine();
            }
        }
        catch (Exception e)
        {
            AlertSender.sendAlert("Unable to read "+in);
            e.printStackTrace();
        }
    }
}
